package backend

import (
	"github.com/gin-gonic/gin"
	"naio/constant"
	"naio/errs"
	"naio/model"
	"naio/serializer"
	"naio/validate"
	"net/http"
	"strconv"
)


func (h *Handler) GridCategoryBrowse(c *gin.Context) {
	var _page = c.DefaultQuery("page", "1")
	var _size = c.DefaultQuery("size", "15")
	page, _ := strconv.Atoi(_page)
	size, _ := strconv.Atoi(_size)
	var cateVOList []constant.BackendGridCategoryResponse
	cateList, total := h.service.GridCategoryService.GetGridCategoryList(page, size)
	for _, v := range *cateList {
		categoryDO := h.service.CategoryService.GetCategoryById(v.CategoryId)
		cateVO := constant.BackendGridCategoryResponse{}
		cateVO.Id = v.Id
		cateVO.Name = v.Name
		cateVO.Picture = v.Picture
		cateVO.CategoryId = v.CategoryId
		cateVO.CategoryName = categoryDO.Name
		cateVOList = append(cateVOList, cateVO)
	}
	data := make(map[string]interface{})
	data["list"] = cateVOList
	data["total"] = total
	res := serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.Data = data
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	c.JSON(http.StatusOK, res)
}


func (h *Handler) GridCategoryFetch(c *gin.Context)  {
	var _id = c.Param("id")
	if _id == "" {
		panic(errs.NewMyAPIError(errs.ErrorParameterValidate, "id参数必填!!!"))
	}

	id, _ := strconv.Atoi(_id)
	gridCategory := h.service.GridCategoryService.GetGridCategoryById(id)
	categoryDO := h.service.CategoryService.GetCategoryById(gridCategory.CategoryId)

	cateVO := constant.BackendGridCategoryResponse{}
	cateVO.Id = gridCategory.Id
	cateVO.Name = gridCategory.Name
	cateVO.Picture = gridCategory.Picture
	cateVO.CategoryId = gridCategory.CategoryId
	cateVO.CategoryName = categoryDO.Name

	res := serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.Data = cateVO
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	c.JSON(http.StatusOK, res)
}


func (h *Handler) GridCategoryUpdate(c *gin.Context) {
	//获取请求参数(json格式)并绑定到struct对象上
	var req constant.BackendGridCategoryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		panic(err)
	}

	//绑定成功,开始验证前台传过来的数据
	validator, _ := validate.Default()
	if check := validator.CheckStruct(req); !check {
		panic(errs.NewMyAPIError(errs.InvalidParams, validator.GetAllErrorString()))
	}
	//通过验证, 说明此时数据合法,接下来验证时间格式
	if req.Id == 0 {
		// add
		if gridCategory := h.service.GridCategoryService.GetGridCategoryByName(req.Name); gridCategory != nil {
			panic(errs.NewMyAPIError(errs.ErrorCategoryExist, "宫格分类已存在"))
		}
		gridCategory := &model.GridCategoryModel{
			Name:       req.Name,
			Picture:    req.Picture,
			CategoryId: req.CategoryId,
			Del:        constant.ZERO,
		}
		h.service.GridCategoryService.AddGridCategory(gridCategory)
	} else{
		// update
		gridCategory := h.service.GridCategoryService.GetGridCategoryById(req.Id)
		if gridCategory.Del == constant.DELETE {
			panic(errs.NewMyAPIError(errs.ErrorCategoryNotExist, "宫格分类不存在"))
		}
		gridCategory.Id = req.Id
		gridCategory.Name = req.Name
		gridCategory.Picture = req.Picture
		gridCategory.CategoryId = req.CategoryId
		gridCategory.Del = req.Del
		h.service.GridCategoryService.UpdateGridCategory(gridCategory)
	}

	res := &serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	c.JSON(http.StatusOK, res)
}

func (h *Handler) GridCategoryDelete(c *gin.Context) {
	var _id = c.Param("id")
	if _id == "" {
		panic(errs.NewMyAPIError(errs.ErrorParameterValidate, "id参数必填!!!"))
	}

	id, _ := strconv.Atoi(_id)
	gridCategory := h.service.GridCategoryService.GetGridCategoryById(id)
	if gridCategory.Del == constant.DELETE {
		panic(errs.NewMyAPIError(errs.ErrorGridCategoryNotExist, "宫格分类不存在"))
	}
	gridCategory.Del = constant.DELETE
	h.service.GridCategoryService.UpdateGridCategory(gridCategory)

	res := serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	res.Data = gridCategory
	c.JSON(http.StatusOK, res)
}


